<template>
  <div class="pageWrapper">
    <common-header />
    <div class="content">
      <div class="card">
        <div class="title">{{ submitForm.taskName }}</div>
        <div class="time">{{ submitForm.year }}年{{ start }} - {{ end }}</div>
      </div>

      <div class="collapse">
        <van-collapse v-model="activeNames">
          <van-collapse-item title-class="collapse-title" value-class="collapse-value" :border="false" title="查看当月周记实"
            name="1">
            <template #value>
              <span v-if="activeNames[0] != '1'">展开</span>
              <span v-else>收起</span>
            </template>
            <div class="collapse-content">
              <div v-for="item in weekReportList" :key="item.id" class="week-item">
                <div class="week-title">{{ item.taskName }}</div>
                <div class="week-content" v-html="item.content.replace(/(\r\n|\n|\r)/gm, '<br />')"></div>
              </div>
              <div class="empty" v-if="!weekReportList.length">暂无数据~</div>
            </div>
          </van-collapse-item>
        </van-collapse>
      </div>

      <div class="card des-wrapper">
        <div class="des-title">记实内容</div>
        <textarea @click="onTextArea" id="content" v-model="submitForm.content" placeholder="请输入记实内容"></textarea>
        <div class="sub-title">附件<span>（最多可传5个）</span></div>
        <uploadFile :delFn="delFile" ref="upload" @fileChange="fileChange" />
      </div>
      <div class="btn-group">
        <div class="btn btn-active" @click="onSubmit(0)">保存</div>
        <div class="btn btn-active" @click="onSubmit(1)">提交</div>
      </div>
    </div>
  </div>
</template>

<script>
import uploadFile from "@/components/uploadFile";
import { Collapse, CollapseItem, Toast } from "vant";
import { monthReportAdd, monthReportUpdate, reportListByMonth, getMonthReportDetailById, delFile } from "@/api/report";
import { formatTime } from "@/utils";
export default {
  components: {
    uploadFile,
    [Collapse.name]: Collapse,
    [CollapseItem.name]: CollapseItem
  },
  data() {
    return {
      activeNames: [],
      submitForm: {
        taskName: "",
        deptId: "",
        year: "",
        month: "",
        content: "",
        isSubmit: 1,
        files: []
      },
      start: "",
      end: "",
      weekReportList: [],
      delFile: delFile
    };
  },
  created() {
    let params = this.$route.query;
    if (params.id) {
      this.submitForm.dataId = params.id;
      this.initData();
    } else {
      this.submitForm.year = +params.year;
      this.submitForm.month = +params.month;
      this.submitForm.deptId = +params.deptId;
      this.submitForm.taskName = `${params.year}年${params.month}月`;
      this.start = params.start;
      this.end = params.end;
      this.getWeekReportList();
    }
  },
  methods: {
    fileChange(file) {
      this.submitForm.files = file.map(ele => {
        let form = {
          fileName: ele.name,
          fileSize: ele.size,
          fileUrl: ele.url
        };
        if (ele.fileId) form.fileId = ele.fileId;
        return form;
      });
    },
    onTextArea() {
      this.$nextTick(() => {
        document.querySelector('#content').focus();
      })
    },
    initData() {
      getMonthReportDetailById(this.submitForm.dataId).then(res => {
        this.submitForm.content = res.data.content;
        this.submitForm.year = res.data.year;
        this.submitForm.month = res.data.month;
        this.submitForm.deptId = res.data.deptId;
        this.submitForm.taskName = res.data.taskName;
        this.submitForm.isPublic = res.data.isPublic;
        this.start = formatTime(res.data.startTime, "MM月dd日");
        this.end = formatTime(res.data.endTime, "MM月dd日");
        this.submitForm.isSubmit = res.data.isSubmit;
        this.submitForm.files = res.data.files;
        this.$refs.upload.setData(
          res.data.files.map(ele => {
            return {
              name: ele.fileName,
              size: ele.fileSize,
              url: ele.fileUrl,
              fileId: ele.fileId
            };
          })
        );
        this.getWeekReportList();
      });
    },
    getWeekReportList() {
      let form = {
        deptId: this.submitForm.deptId,
        year: this.submitForm.year,
        month: this.submitForm.month
      };
      reportListByMonth(form).then(res => {
        this.weekReportList = res.data;
      });
    },
    onSubmit(val) {
      if (!this.submitForm.content) {
        Toast("请输入记实内容");
        return;
      }
      let submitFn = this.submitForm.dataId ? monthReportUpdate : monthReportAdd;
      this.submitForm.isSubmit = val;
      submitFn(this.submitForm).then(res => {
        if (res.code == 200) {
          Toast(this.submitForm.dataId ? "编辑成功" : "添加成功");
          this.$router.back();
        } else {
          Toast(res.msg);
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.content {
  padding: 20px 16px;

  .card {
    padding: 16px;
    background: #fff;
    border-radius: 8px;

    .title {
      line-height: 20px;
      color: #333;
      font-size: 14px;
    }

    .time {
      line-height: 17px;
      color: #a3a7ad;
      font-size: 12px;
      margin-top: 3px;
    }
  }
}

.collapse {
  margin-top: 12px;
}

.des-wrapper {
  margin-top: 12px;

  .des-title {
    padding-top: 4px;
    font-size: 16px;
    color: #000;
    line-height: 22px;
  }

  textarea {
    width: 100%;
    padding: 0;
    color: #333;
    line-height: 20px;
    margin-top: 16px;
    height: 136px;
    resize: none;
    border: none;
    font-size: 14px;

    &::placeholder {
      color: #b2b2b2;
    }
  }
}

.sub-title {
  position: relative;
  margin-top: 16px;
  margin-bottom: 12px;
  color: #333333;
  padding-left: 12px;
  font-size: 14px;

  span {
    color: #cccccc;
  }

  &::before {
    content: "";
    position: absolute;
    width: 4px;
    height: 14px;
    top: 2px;
    left: 0;
    border-radius: 1px;
    background: #367fff;
  }
}

.btn-group {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 35px 0;

  .btn {
    width: 164px;
    text-align: center;
    line-height: 54px;
    border-radius: 12px;
    background: #2a81e6;
    color: #fff;
    font-size: 14px;

    &:first-child {
      color: #2a81e6;
      background: #fff;
    }
  }
}

.collapse {
  border-radius: 8px;
  overflow: hidden;
}

.empty {
  text-align: center;
}

::v-deep .van-collapse-item__title--borderless {
  line-height: 48px;
  padding: 0 16px;

  .van-cell__title {
    font-size: 14px;
    color: #333;
  }

  .van-cell__value {
    font-size: 12px;
    color: #b3b3b3;
  }

  .van-cell__right-icon {
    line-height: 48px;
    color: #2a81e6;
  }
}

::v-deep .van-collapse-item__content {
  padding: 4px 16px 16px;
}

.week-item {
  margin-bottom: 16px;
  line-height: 22px;

  &:last-child {
    margin-bottom: 0;
  }
}

.week-title {
  color: #4d4d4d;
}

.week-content {
  color: #808080;
  word-break: break-all;
}
</style>
